const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ParallelUglifyPlugin = require('webpack-parallel-uglify-plugin');
const ModuleConcatenationPlugin = require('webpack/lib/optimize/ModuleConcatenationPlugin');
const HappyPack = require('happypack');
module.exports = {
    // entry: './src/index.js', entry: {     pageA: './src/pageA',     pageB:
    // './src/pageB' },
    entry: './src/index2.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: '[name]_[hash:8].js',
        chunkFilename: '[name].js',
        //publicPath: 'http://img.zhufengpeixun.cn'
    },

    resolve: {
        modules: [path.resolve(__dirname, 'node_modules')],
        //mainFields: ['main'],
        mainFields: [
            'jsnext:main', 'browser', 'main'
        ],
        /*alias: {
            'react': path.resolve(__dirname, './node_modules/react/cjs/eact.production.min.js')
        },*/
        extensions: ['.js']
    },
    devServer: {
        contentBase: './dist',
        inline: true,
        port: 8081,
        hot: true
    },
    watch: false, //只有在开启监听模式时，watchOptions才有意义
    watchOptions: {
        ignored: /node_modules/,
        aggregateTimeout: 300, //监听到变化发生后等300ms再去执行动作，防止文件更新太快导致编译频率太高
        poll: 1000 //通过不停的询问文件是否改变来判断文件是否发生变化，默认每秒询问1000次
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                //把对.js文件的处理转交给id为babel的HappyPack实例
                use: 'happypack/loader?id=babel',
                include: path.resolve(__dirname, 'src'),
                exclude: /node_modules/
            }, {
                //把对.css文件的处理转交给id为css的HappyPack实例
                test: /\.css$/,
                use: 'happypack/loader?id=css',
                include: path.resolve(__dirname, 'src')
            }, {
                //把对.css文件的处理转交给id为css的HappyPack实例
                test: /\.(jpg|png|gif|svg)$/,
                use: 'url-loader',
                include: path.resolve(__dirname, 'src')
            }
        ],
        noParse: [/react\.min\.js/]
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    chunks: "initial",
                    minChunks: 2,
                    maxInitialRequests: 5, // The default limit is too small to showcase the effect
                    minSize: 0 // This is example is too small to create commons chunks
                },
                vendor: {
                    test: /node_modules/,
                    chunks: "initial",
                    name: "vendor",
                    priority: 10,
                    enforce: true
                }
            }
        }
    },
    plugins: [
        new ModuleConcatenationPlugin(), new webpack.NamedModulesPlugin(),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.DllReferencePlugin({
            manifest: require(path.join(__dirname, 'dist', 'react.manifest.json'))
        }),
        new HtmlWebpackPlugin({template: './src/index.html'}),
        //用唯一的标识符id来代表当前的HappyPack是用来处理一类特定文件
        new HappyPack({
            id: 'babel',
            //如何处理.js文件，和rules里的配置相同
            loaders: [
                {
                    loader: 'babel-loader',
                    query: {
                        presets: [
                            [
                                "env", {
                                    modules: false
                                }
                            ],
                            "stage-0",
                            "react"
                        ]
                    }
                }
            ]
        }),
        new HappyPack({
            id: 'img',
            //如何处理.js文件，和rules里的配置相同
            loaders: [
                {
                    loader: 'url-loader?name=[name]_[hash:8].[ext]'
                }
            ]
        }),
        new webpack.DefinePlugin({
            'process.env': {
                NODE_ENV: JSON.stringify('production')
            }
        }),
        new HappyPack({
            id: 'css',
            loaders: [
                'style-loader', 'css-loader?minimize'
            ],
            threads: 4, //代表开启几个子进程去处理这一类型的文件
            verbose: true //是否允许输出日子
        }),

        // new ParallelUglifyPlugin({     workerCount: 3, //开启几个子进程去并发的执行压缩。默认是当前运行电脑的
        // CPU 核数减去1     uglifyJS: {         output: {             beautify: false,
        // //不需要格式化             comments: false, //不保留注释         },         compress: {
        // warnings: false, // 在UglifyJs删除没有用到的代码时不输出警告 drop_console: true, // 删除所有的
        // `console` 语句，可以兼容ie浏览器 collapse_vars: true, // 内嵌定义了但是只用到一次的变量 reduce_vars:
        // true, // 提取出出现多次但是没有定义成变量去引用的静态值         }     } })
    ],
    devtool: 'inline-source-map'
}